package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.pojo.ApiResult;
import com.exam.pojo.Student;
import com.exam.service.StudentService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询学生信息
    @GetMapping("/students/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page,@PathVariable Integer size) {
        Page<Student> studentPage = new Page<>(page,size);
        IPage<Student> res = studentService.findAll(studentPage);
//        System.out.println(studentPage.getCurrent()+""+studentPage.getSize());
        return  ApiResultHandler.buildApiResult(200,"分页查询所有学生",res);
    }
    @GetMapping("/student/{studentId}")
    public ApiResult findStudentById(@PathVariable("studentId") Integer studentId) {
        Student res = studentService.findStudentById(studentId);
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        } else {
            return ApiResultHandler.buildApiResult(404,"查询的用户不存在",null);
        }
    }

    @DeleteMapping("/student/{studentId}")
    public ApiResult deleteStudentById(@PathVariable("studentId") Integer studentId) {
        studentService.deleteStudentById(studentId);
        return ApiResultHandler.buildApiResult(200,"删除成功", null);
    }


    @PutMapping("/studentPWD")
    public ApiResult updatePwd(@RequestBody Student student) {
        int res=studentService.updatePwd(student);
        return ApiResultHandler.buildApiResult(200,"密码更新成功",null);
    }
    @PutMapping("/student")
    public ApiResult updateStudent(@RequestBody Student student) {
        int res = studentService.updateStudent(student);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"更新成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"更新失败",res);
    }

    @PostMapping("/student")
    public ApiResult addStudent(@RequestBody Student student) {
        int res = studentService.addStudent(student);
        if (res == 1) {
            return ApiResultHandler.buildApiResult(200,"添加成功",null);
        }else {
            return ApiResultHandler.buildApiResult(400,"添加失败",null);
        }
    }

}
